
use Data::Dumper;

use Model;

use User;

use Config::IniFiles;
use Clone qw(clone);

use YAML::XS;

sub idata2hash {
	my $idata= shift;
	my $hash={};
	my @parent_stack;
	my $v;
	print "IDATA: ".Dumper($idata);
	push(@parent_stack,$hash);
	my @istack;
	foreach $i (@$idata) {
	   if($i->{'what'} eq 'data') {
		if($i->{'indent'} == 0) {		 
		    $v=$i->{'value'};
		    $parent_stack[@parent_stack-1]->{$v} = { };
		    push(@istack,$i);
		}elsif($i->{'indent'} == 
		       ($istack[@istack-1]->{'indent'} + 1) ) {
			my $rec = clone ($i);
			$rec->{'guid'}= getGUID();
			$v=$i->{'value'};
			push(@{$parent_stack[@parent_stack-1]}, $v);
		}elsif($i->{'indent'} == 
		       ($istack[@istack-1]->{'indent'} ) )  {
		    my $topo=pop(@parent_stack);
		    my $topi=pop(@istack);
		    my $rec = clone ($i);
		    $rec->{'guid'}= getGUID();
		    $v=$topi->{'value'};
		    push(@{$hash->{$v}},$rec);
		}else{
		    die "sys error indent at ".Dumper($i);
		}
		print "\n\n-IDATA:".Dumper($i). "\n-HASH: ".Dumper($hash);
	   }
	}
	return $hash;
}

sub read_me_sys {
	my $root = shift;
	my $me_db = shift;
	foreach $id (@_) {
	    die "read_me_sys: already defined: $id" if(defined($me_db->{$id}));
	    my $fullfname = $root."/".$id."/.ME.INI";
	    my $ini = Config::IniFiles->new( -file => $fullfname );
	    die "already defined configuration for $id" if(defined($me_db->{$id}));
	    $me_db->{$id} = { };
	    foreach $sectionName (keys %ini ) {	      
		$me_db->{$id}->{$sectionName} = { };
		while (($paramName, $paramValue) = each %{$ini{$sectionName}}) {
		    $me_db->{$id}->{$sectionName}->{$paramName} = $paramValue;
		}
	    }	  
	    
	}
	return $me_db;
}


# my $idata2 = read_raw_ifile("./data/REQ/TOOL/GEN/.ME.SYS");


# my $model = read_model("./data/MODEL.CONF");

#my $me_sys = read_me_sys("./data",{ } , "","REQ","REQ/TOOL","REQ/TOOL/GEN",
#			"REQ/TOOL/GEN/002","REQ/TOOL/GEN/001");





